package top.gbiot.control.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.gbiot.common.core.domain.AjaxResult;
import top.gbiot.common.exception.base.BaseException;
import top.gbiot.config.mqtt.MqttPushClient;

/**
 * /remote_control/switch/shade_net/
 * 远程控制
 */
@RestController
@RequestMapping("/remote_control")
public class RemoteControlController {

    private static final Logger log = LoggerFactory.getLogger(RemoteControlController.class);

    @Autowired
    private MqttPushClient mqttPushClient;

    /**
     * 水泵控制
     * @param alert_value
     * @return
     */
    @PostMapping("/switch/irrigation_pump/{alert_value}")
    public AjaxResult switchIrrigationPump(@PathVariable Long alert_value) {
        System.out.println("水泵控制:"+alert_value);
        String msg ="";
        if (alert_value==1L){
            msg="irrigationPump/off";
        }else if(alert_value==2L){
            msg="irrigationPump/on";
        }else{
            throw new BaseException("水泵控制失败，请重新操作！");
        }
        System.out.println(msg);
        mqttPushClient.publish(0,false,"test/cmd",msg);
        System.out.println("发送成功");
        return new AjaxResult(AjaxResult.Type.SUCCESS,"水泵操作成功!");
    }

    /**
     * 窗帘控制
     * @param alert_value
     * @return
     */
    @PostMapping("/switch/shade_net/{alert_value}")
    public AjaxResult switchShadeNet(@PathVariable Long alert_value) {
        System.out.println("窗帘控制:"+alert_value);
        String msg ="";
        if (alert_value==1L){
            msg="shadeNet/off";
        }else if(alert_value==2L){
            msg="shadeNet/on";
        }else{
            throw new BaseException("窗帘控制失败，请重新操作！");
        }
        System.out.println(msg);
        mqttPushClient.publish(0,false,"test/cmd",msg);
        System.out.println("发送成功");
        return new AjaxResult(AjaxResult.Type.SUCCESS,"窗帘操作成功!");
    }

    /**
     * 增氧泵控制
     * @param alert_value
     * @return
     */
    @PostMapping("/switch/oxygen_pump/{alert_value}")
    public AjaxResult switchOxygenPump(@PathVariable Long alert_value) {
        System.out.println("增氧泵控制:"+alert_value);
        String msg ="";
        if (alert_value==1L){
            msg="oxygenPump/off";
        }else if(alert_value==2L){
            msg="oxygenPump/on";
        }else{
            throw new BaseException("增氧泵控制失败，请重新操作！");
        }
        System.out.println(msg);
        mqttPushClient.publish(0,false,"test/cmd",msg);
        System.out.println("发送成功");
        return new AjaxResult(AjaxResult.Type.SUCCESS,"增氧泵操作成功!");
    }

    /**
     * 空气净化器控制
     * @param alert_value
     * @return
     */
    @PostMapping("/switch/twyer/{alert_value}")
    public AjaxResult switchTwyerp(@PathVariable Long alert_value) {
        System.out.println("空气净化器控制:"+alert_value);
        String msg ="";
        if (alert_value==1L){
            msg="twyer/off";
        }else if(alert_value==2L){
            msg="twyer/on";
        }else{
            throw new BaseException("空气净化器控制失败，请重新操作！");
        }
        System.out.println(msg);
        mqttPushClient.publish(0,false,"test/cmd",msg);
        System.out.println("发送成功");
        return new AjaxResult(AjaxResult.Type.SUCCESS,"空气净化器操作成功!");
    }

}
